/**
 * 数据字典
 */
const mongoose = require('mongoose')
const shortid = require('shortid')
const moment = require('moment')
const GroupType = require('../groupType/models.js')
const Role = require('../role/models.js')
const Schema = mongoose.Schema

const GroupSchema = new Schema({
  // 数据库中存储ID
  _id: {
    type: String,
    default: shortid.generate
  },
  groupname:String,// 编码
  groupcode: String,    // 名称
  groupphone : {     // 联系电话号码
    type: String,
    default: ''
  },
  groupuser: {    // 组织法人
    type: String,
    default: ''
  },
  grouptype:{
    type: String,
    ref:'GroupType'
  },
  description: String,    // 描述
  roles:[
    {
      type: String,
      ref: 'Role'
    }
  ],
  // 是否激活
  enable: {
    type: Boolean,
    default: true
  },
  create_time: {
    type: Date,
    default: Date.now
  }
})

GroupSchema.set('toJSON', { getters: true, virtuals: true })
GroupSchema.set('toObject', { getters: true, virtuals: true })

GroupSchema.path('create_time').get(function (v) {
  return moment(v).utc().utcOffset(-8).format("YYYY-MM-DD")
})

module.exports = mongoose.model('Group', GroupSchema)